package com.whfc.ms.api.emp.controller;

import com.whfc.common.result.ListData;
import com.whfc.common.result.Result;
import com.whfc.common.result.ResultUtil;
import com.whfc.common.util.SessionAttr;
import com.whfc.emp.dto.AppEmpContractDTO;
import com.whfc.emp.param.AppContractAddParam;
import com.whfc.emp.param.AppContractEditParam;
import com.whfc.entity.dto.OssPathDTO;
import com.whfc.fuum.entity.SysUser;
import com.whfc.ms.api.emp.service.MsAppContractService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author sun_guodong
 * @description 人员合同
 * @date 2020-08-13
 */
@RestController
@RequestMapping("/ms/api/emp/contract")
public class AppContractController {
    
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private MsAppContractService msAppContractService;

    @GetMapping("/list")
    public Result list(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                       @RequestParam("empId") Integer empId) {
        logger.info("合同列表,loginer:{},empId:{}", user.getUsername(), empId);
        ListData<AppEmpContractDTO> listData = msAppContractService.list(empId);
        return ResultUtil.success(listData);
    }

    @PostMapping("/add")
    public Result add(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                      @Validated @RequestBody AppContractAddParam param) {
        logger.info("添加合同,loginer:{},param:{}", user.getUsername(), param.toString());
        msAppContractService.add(param);
        return ResultUtil.success();
    }

    @PostMapping("/edit")
    public Result edit(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                       @Validated @RequestBody AppContractEditParam param) {
        logger.info("编辑合同,loginer:{},param:{}", user.getUsername(), param.toString());
        msAppContractService.edit(param);
        return ResultUtil.success();
    }

    @PostMapping("/del/{id}")
    public Result del(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                      @PathVariable("id") Integer id) {
        logger.info("删除合同,loginer:{},id:{}", user.getUsername(), id);
        msAppContractService.del(id);
        return ResultUtil.success();
    }

    @PostMapping("/upload")
    public Result upload(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                         @RequestParam(value = "base64Str", required = false) String base64Str,
                         @RequestParam(value = "file", required = false) MultipartFile file) throws Exception {
        logger.info("上传合同附件图片,loginer:{}", user.getUsername());
        OssPathDTO path = msAppContractService.upload(base64Str, file);
        return ResultUtil.success(path);
    }

}
